home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: Anagram module
- *
- * 19 July 1986 Paul DuBois
- *
- * 24 Dec 93
- * - Added checking to see whether current state of answer is correct. Dims
- * Answer button if so. This gives some feedback that the response is correct,
- * which has been lacking. Otherwise user has to recognize the word.
- */
-
- # include "TransSkel.h"
-
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- # define vWord 10
- # define letterSize 18
- # define letterGap 2
-
-
- static WindowPtr wind;
- static BlobSetHandle receptors = (BlobSetHandle) nil;
- static short hMid;
- static ControlHandle next;
- static ControlHandle answer;
-
-
- /*
- * Check current state of answer. If all receptors have a glob that's
- * the correct letter, return true. Can't just check whether BGlob (b) = b,
- * since different blobs may have same letter, and it doesn't matter which
- * of those is glued.
- */
-
- static Boolean
- CheckAnswer (void)
- {
- BlobHandle b, g;
-
- for (b = FirstBlob (receptors); b != (BlobHandle) nil; b = NextBlob (b))
- {
- g = BGlob (b);
- if (GetBRefCon (b) != GetBRefCon (g))
- return (false); /* wrong letter glued to blob */
- }
- return (true);
- }
-
-
- /*
- * Generate the next anagram. Get a word from the word picker,
- * put each letter in a receptor and glue the receptor to itself.
- * Then scramble the receptors and show them.
- */
-
- static void
- NextAnagram (void)
- {
- BlobHandle b;
- short i, h;
- char *s;
-
- if (receptors != nil) /* dispose of any previous sets */
- {
- HideBlobSet (receptors);
- DisposeBlobSet (receptors);
- }
- receptors = NewBlobSet ();
- s = (char *) PickWord ();
- h = hMid - (s[0] * (letterSize + letterGap) - letterGap) / 2;
- for (i = 1; i <= s[0]; ++i)
- {
- b = MakeCharBlob (receptors, false, infiniteGlue,
- false, h, vWord, s[i]);
- h += letterSize + letterGap;
- GlueGlob (b, b);
- }
- for (;;) /* shuffle so that answer isn't correct right away */
- {
- ShuffleGlobSet (receptors);
- if (!CheckAnswer ())
- break;
- }
- ShowBlobSet (receptors);
- HiliteControl (answer, normalHilite);
- InvalRect (&wind->portRect);
- }
-
-
- /*
- * Show answer to anagram. This may not be unique. For instance,
- * "begin" can also be "being". Clearing the globs shows the original
- * attachments, as well as disabling any further transactions until
- * the Next button is hit, since there won't be any globs to swap.
- */
-
- static void
- ShowAnswer (void)
- {
- UnglueGlobSet (receptors);
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- BlobHandle b, d;
- ControlHandle ctl;
-
- if (FindControl (pt, wind, &ctl))
- {
- if (TrackControl (ctl, pt, nil)) /* button hit? */
- {
- if (ctl == next)
- NextAnagram ();
- else if (ctl == answer)
- {
- ShowAnswer ();
- HiliteControl (answer, dimHilite);
- }
- }
- }
- else if ((**answer).contrlHilite == normalHilite) /* answer not correct yet */
- {
- BlobClick (pt, t, nil, receptors);
- if (CheckAnswer ())
- HiliteControl (answer, dimHilite);
- }
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- DrawControls (wind);
- DrawBlobSet (receptors);
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (false, false, false, true, true);
- }
- }
-
-
- void
- AnagramInit (void)
- {
- Rect r;
-
- SkelWindow (wind = GetDemoWind (anaWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- hMid = wind->portRect.right / 2;
- SetRect (&r, 0, 0, 80, 20);
- OffsetRect (&r, hMid - 40 - 90 / 2, wind->portRect.bottom - 25);
- next = NewControl (wind, &r, "\pNext", true, 0, 0, 0,
- pushButProc, 0L);
- OffsetRect (&r, 90, 0);
- answer = NewControl (wind, &r, "\pAnswer", true, 0, 0, 0,
- pushButProc, 0L);
-
- SetCharBlobSize (letterSize);
- NextAnagram ();
-
- MakeFrontWind (wind);
- }
-